package com.glodon.ggzy.fwpt.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.Constants;
import com.glodon.ggzy.core.utils.JsonUtils;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.QuerySupport;
import com.glodon.ggzy.core.utils.Tools;
import com.glodon.ggzy.entity.GgCqZbcjgg;
import com.glodon.ggzy.entity.GgJsProjectInfo;
import com.glodon.ggzy.entity.GgJsWinningPublic;
import com.glodon.ggzy.entity.GgTdGs;
import com.glodon.ggzy.entity.GgZfBulletin;
import com.glodon.ggzy.fwpt.service.ICqjyProjectService;
import com.glodon.ggzy.fwpt.service.IJsgcProjectService;
import com.glodon.ggzy.fwpt.service.ITdfcProjectService;
import com.glodon.ggzy.fwpt.service.IZfcgProjectService;
import com.glodon.ggzy.vo.JsgcInfoVO;
import com.glodon.ggzy.vo.Xxgk;
import com.glodon.ggzy.vo.ZfcgInfoVO;

/**
 * 
 * 中标公示处理控制器
 * @author yangjf
 * @date 2013/06/12
 */
@Controller
public class ZbgsController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IJsgcProjectService jsgcProjectService;

	@Autowired
	private IZfcgProjectService zfcgProjectService;

	@Autowired
	private ICqjyProjectService cqjyProjectService;

	@Autowired
	private ITdfcProjectService tdfcProjectService;

	/**
	 * 
	 * 中标公示-建设工程 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_jsgc_page_list")
	public String xzclJsgcPageList(Model model) {
		return "/fwpt/zbgs_jsgc_page_list";
	}

	/**
	 * 
	 * 中标公示-建设工程 分页数据加载
	 * @param model
	 * @param rows
	 * @param page
	 * @param response
	 */
	@RequestMapping("/fwpt/zbgs_jsgc_page_data")
	public void getZbgg_JsgcPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		Page queryPage = jsgcProjectService.queryJsgcWinningPublic(qm, searchText);
		List<?> prjList = queryPage.getResult();
		List<JsgcInfoVO> zbggList = new ArrayList<JsgcInfoVO>();
		for (int i = 0; i < prjList.size(); i++) {
			Object[] object = (Object[]) prjList.get(i);
			GgJsProjectInfo project = (GgJsProjectInfo) object[0];
			GgJsWinningPublic winning = (GgJsWinningPublic) object[1];
			JsgcInfoVO jsgcinfo = new JsgcInfoVO(winning.getId(), project.getProjectName(), project.getProjectType(),
					Tools.formatDateToString(winning.getPublicStartTime()), project.getId());
			zbggList.add(jsgcinfo);
		}
		queryPage.setResult(zbggList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 中标公示-政府采购 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_zfcg_page_list")
	public String xzclZfcgPageList(Model model) {
		return "/fwpt/zbgs_zfcg_page_list";
	}

	/**
	 * 
	 * 中标公示-产权交易 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_cqjy_page_list")
	public String xzclCqjyPageList(Model model) {
		return "/fwpt/zbgs_cqjy_page_list";
	}

	/**
	 * 获取招标公告-产权交易详情:分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_cqjy_page_data")
	public void getZbgsCqjyPageList(Model model, int rows, int page, String prjName, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (StringUtils.isNotEmpty(prjName)) {
			try {
				qm.addParamToMap("name_like", "%" + URLDecoder.decode(prjName, "UTF-8") + "%");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		qm.addParamToMap(QuerySupport.QUERYCLASS, GgCqZbcjgg.class);
		qm.addParamToMap(QuerySupport.ORDERBYDESC, "publishDate");
		qm.addParamToMap("publishStatus", "1");
		//queryModel.addParamToMap("prjName_is_not_null", null);
		//queryModel.addParamToMap("prjNo_like", "%1%");
		//queryModel.addParamToMap("prjName|prjNo", "1");
		//queryModel.addParamToMap("prjName_like|prjNo_like", "%1%");
		Page queryPage = cqjyProjectService.queryPage(qm);

		List<GgCqZbcjgg> zbggList = (List<GgCqZbcjgg>) queryPage.getResult();
		List<Xxgk> resultList = new ArrayList<Xxgk>();
		Xxgk xx = null;
		for (GgCqZbcjgg gg : zbggList) {
			xx = new Xxgk(gg.getName(), gg.getId(), Tools.formatDateToString(gg.getPublishDate()));
			resultList.add(xx);
		}
		queryPage.setResult(resultList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取招标公告-产权交易详情:分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/tbgs_cqjy_page_data")
	public ModelAndView getZbggCqjyPageList(Model model, int rows, int page, String pkgName, HttpServletResponse response) {
		if(StringUtils.isEmpty(pkgName))pkgName = ""; 
		return new ModelAndView("redirect:/fwpt/xxgk_cqjy_page_data.do?rows=" + rows + "&page=" + page + "&prjName=" + pkgName + "&prjType=tbgs");
	}

	/**
	 * 
	 * 中标公示-土地房产 分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_tdfc_page_list")
	public String xzclTdfcPageList(Model model) {
		return "/fwpt/zbgs_tdfc_page_list";
	}

	/**
	 * 获取中标公示-土地房产分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_tdfc_page_data")
	public void getZbgs_TdfcPageList(Model model, String gsName, int rows, int page, HttpServletResponse response) {

		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);

		String name = "";
		if (StringUtils.isNotEmpty(gsName)) {
			try {
				name = "%" + URLDecoder.decode(gsName, "UTF-8") + "%";
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				name = "";
			}
		}
		
		if (StringUtils.isNotEmpty(name)) {
			qm.addParamToMap("gsName_like", name);
		}
		qm.addParamToMap(QuerySupport.QUERYCLASS, GgTdGs.class);
		qm.addParamToMap(QuerySupport.ORDERBYDESC, "gsUploadtime");
		Page queryPage = tdfcProjectService.queryXxgkTdfcPage(qm);
		List<GgTdGs> prjList = (List<GgTdGs>) queryPage.getResult();

		List<Xxgk> peList = new ArrayList<Xxgk>();

		for (GgTdGs prj : prjList) {
			Xxgk xx = new Xxgk(prj.getGsName(), prj.getGsNo(), prj.getId(), null, null);
			peList.add(xx);
		}
		
		queryPage.setResult(peList);

		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 中标公示-建设工程集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_jsgc_list")
	public String getZbgsJsgcList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/zbgs_jsgc_list";
	}

	/**
	 * 中标公示-建设工程详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_jsgc_detail")
	public String getZbgsJsgcDetail(String id, Model model) {
		GgJsWinningPublic winningPublic = jsgcProjectService.getGgJsWinningPublicById(id);
		GgJsProjectInfo projectInfo = jsgcProjectService.getGgJsProjectInfoById(winningPublic.getProjectId());
		model.addAttribute("projectInfo", projectInfo);
		model.addAttribute("winningPublic", winningPublic);
		model.addAttribute("id", id);
		return "/fwpt/zbgs_jsgc_detail";
	}

	/**
	 * 中标公示-政府采购集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_zfcg_list")
	public String getZbgsZfcgList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/zbgs_zfcg_list";
	}

	/**
	 * 中标公示-政府采购详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_zfcg_detail")
	public String getZbgsZfcgDetail(String id, Model model) {
		GgZfBulletin bulletin = zfcgProjectService.getGgZfBulletinById(id);
		model.addAttribute("bulletin", bulletin);
		return "/fwpt/zbgs_zfcg_detail";
	}

	/**
	 * 获取招标公示-政府采购详情:分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_zfcg_page_data")
	public void getZbgsZfcgPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		//查查参数赋值
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		Page queryPage = zfcgProjectService.queryZfcgTenderNotice(qm, searchText, Constants.FWPT_ZBGS);
		List<GgZfBulletin> noticeList = (List<GgZfBulletin>) queryPage.getResult();
		List<ZfcgInfoVO> xxgkList = new ArrayList<ZfcgInfoVO>();
		for (GgZfBulletin bulletin : noticeList) {
			ZfcgInfoVO xxgk = new ZfcgInfoVO(bulletin.getId(), bulletin.getBulletincode(), bulletin.getBulletinname(), bulletin.getStockmode(),
					bulletin.getBuysarttime());
			xxgkList.add(xxgk);
		}
		queryPage.setResult(xxgkList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 中标公示-产权交易集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_cqjy_list")
	@Deprecated
	public String getZbgsCqjyList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/zbgs_cqjy_list";
	}

	/**
	 * 中标公示-产权交易详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_cqjy_detail")
	public String getZbgsCqjyDetail(String prjId, Model model) {
		model.addAttribute("detail", null);
		return "/fwpt/zbgs_cqjy_detail";
	}

	/**
	 * 中标公示-土地房产集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_tdfc_list")
	public String getZbgsTdfcList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/zbgs_tdfc_list";
	}

	/**
	 * 中标公示-土地房产详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgs_tdfc_detail")
	public String getZbgsTdfcDetail(String id, Model model) {

		GgTdGs queryGgTdGsById = tdfcProjectService.queryGgTdGsById(id);
		
		model.addAttribute("detail", queryGgTdGsById.getGsText());
//		String proId = queryGgTdGsById.getPrjId();
//
//		//写业务逻辑
//		List<ZbgsResultVO> queryZbgsResult = tdfcProjectService.queryZbgsResultList(proId);
//
//		//获取公告信息
//		GgTdGg notice = tdfcProjectService.queryGgTdGgByProId(proId);
//
//		GgTdGs ggTdGs = tdfcProjectService.queryGgTdGsByProId(proId);
//
//		model.addAttribute("gpEndtime", TimeUtil.DateToCN(notice.getGpEndtime()));
//		model.addAttribute("gsUploadtime", TimeUtil.DateToCN(ggTdGs.getGsUploadtime()));
//		model.addAttribute("list", queryZbgsResult);

		return "/fwpt/zbgs_tdfc_detail";
	}
}
